package com.zhucan.autoconfigure.xxl;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "xxl.job", name = "enable", havingValue = "true",matchIfMissing = true)
@EnableConfigurationProperties(XxlProperties.class)
public class XxlAutoConfiguration {

  protected Logger logger = LoggerFactory.getLogger(this.getClass());

  @Autowired
  private XxlProperties properties;

  @Bean(initMethod = "start", destroyMethod = "destroy")
  public XxlJobSpringExecutor xxlJobExecutor() {
    logger.debug(">>>>>>>>>>> xxl-job config init.");
    XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
    xxlJobSpringExecutor.setAdminAddresses(properties.getAdminUrl());
    xxlJobSpringExecutor.setAppName(properties.getExecutor().getAppName());
    xxlJobSpringExecutor.setIp(properties.getExecutor().getIp());
    xxlJobSpringExecutor.setPort(properties.getExecutor().getPort());
    xxlJobSpringExecutor.setAccessToken(properties.getAccessToken());
    xxlJobSpringExecutor.setLogPath(properties.getExecutor().getLogPath());
    xxlJobSpringExecutor.setLogRetentionDays(properties.getExecutor().getLogRetentionDays());

    return xxlJobSpringExecutor;
  }
}